#coding=utf-8
# 导入模板
import pygame
from pygame.locals import *

import time
import random

# 子弹类
class Bullet(object):
    # 构造方法
    def __init__(self, plane):
        # False为敌机的子弹
        self.side = plane.side
        
        
        if self.side:
            # 坐标
            self.x = plane.x + 40
            self.y = plane.y - 20
            # 图片
            self.image = pygame.image.load("./images/player_bullet.png")
        else:
            # 坐标
            self.x = plane.x + 22
            self.y = plane.y + 37
            self.image = pygame.image.load("./images/enemy_bullet.png")
            
    # 子弹移动
    def move(self):
        if self.side:
            self.y -= 4
        else:
            self.y += 4
            

# 飞机类
class Plane(object):
    # 构造函数
    def __init__(self, side):
        # False为敌机
        self.side = side
        if self.side:
            # 坐标
            self.x = 160
            self.y = 400
            # 图片
            self.image = pygame.image.load("./images/player.png")
        else:
            # 坐标
            self.x = 190
            self.y = 100
            # 图片
            self.image = pygame.image.load("./images/enemy.png")
            
        # 子弹列表
        self.bulletList = []
    
    # 玩家键盘控制移动
    def player(self, key):
        if key == K_UP :
            if self.y > 0 :
                self.y -= 20

        elif key == K_DOWN :
            if self.y < 580 :
                self.y += 20

        elif key == K_LEFT :
            if self.x > 0 :
                self.x -= 20

        elif key == K_RIGHT :
            if self.x < 320:
                self.x += 20
                
        elif key == K_SPACE :
            # 空格发射子弹
            bullet = Bullet(self)
            self.bulletList.append(bullet)
                
    
    # 敌机自动移动，自动发射子弹
    def enemy(self):
        direction = random.randint(1, 400)
        if direction > 0 and direction <= 10 :
            # 上
            if self.y > 0 :
                self.y -= 10
        elif direction > 100 and direction <= 110 :
            # 下
            if self.y < 660 :
                self.y += 10
        elif direction > 200 and direction <= 210 :
            # 左
            if self.x > 0 :
                self.x -= 10
        elif direction > 300 and direction <= 310 :
            # 右
            if self.x < 370 :
                self.x += 10
                    
        if random.randint(1, 100) == 50:
            bullet = Bullet(self)
            self.bulletList.append(bullet)
            
    # 显示飞机和子弹
    def display(self, window):
        # 将飞机图片添加到窗体
        window.blit(self.image, (self.x, self.y))
        
        for bullet in self.bulletList:
            # 子弹移动
            bullet.move()
            # 将子弹图片添加到窗体
            window.blit(bullet.image, (bullet.x, bullet.y))
            if bullet.y < 0 or bullet.y > 700 :
                # 把不在地图中的子弹删除
                self.bulletList.remove(bullet)


# 飞机大战游戏
if __name__ == '__main__':
    # 创建窗体，设置大小
    window = pygame.display.set_mode((420, 700), 0, 32)
    # 加载背景图片
    background = pygame.image.load("./images/background.png")
    # 创建玩家飞机
    player = Plane(True)
    # 创建玩家飞机
    enemy = Plane(False)
    
    while True:
        # 将背景图片添加到窗体
        window.blit(background, (0, 0))
        
        # 显示
        player.display(window)
        enemy.display(window)
        
        # 事件
        for event in pygame.event.get():
            if event.type == QUIT :
                exit()
            elif event.type == KEYDOWN :
                player.player(event.key)
                #enemy.player(event.key)
                
        # 敌机
        enemy.enemy()
        # 更新窗口
        pygame.display.update()
        # 睡眠
        time.sleep(0.01)